namespace :uploads do
  desc "Pull down the uploads directory"
  task :pull do
    directory = File.join("wp-content", "uploads")
    
    local_path = File.join(Dir.pwd, directory)
    remote_path = File.join(release_path, directory)
    
    on roles(:app) do |server|
      unless test("[ -d #{remote_path} ]")
        error "There isn't a #{directory} directory on #{server.user}@#{server.hostname}"
        
        next
      end
      
      unless fetch(:confirm_pull_uploads)
        set :confirm_pull_uploads, ask("confirmation for local #{directory} directory overwrite", "Y/n")
      end
    
      next unless true == fetch(:confirm_pull_uploads) or ["y", "yes"].include? fetch(:confirm_pull_uploads).downcase
      
      run_locally do
        execute :mkdir, "-p", local_path
      end
      
      info "Pulling #{directory} directory from #{server.user}@#{server.hostname}"
      
      # Fix for rsync
      remote_path += "/"
      
      run_locally do
        execute :rsync, "-lrtvzO", "--delete-before", (server.port ? "-e 'ssh -p #{server.port}'" : nil), "#{server.user}@#{server.hostname}:#{remote_path}", local_path
      end
    end
  end
end
